using System;
using System.Collections.Generic;
using System.Text;

namespace BAFIRCSharp
{
    public enum CTCPType
    {
        CTCPQuery,
        CTCPResponse
    }

    public class CTCPMessage
    {
        // See if a message is CTCP encoded
        public static bool IsCTCP(string Message)
        {
            //if (Message.StartsWith('\x01') && Message.EndsWith('\x01'))
            if(Message[0] == 1 && Message[Message.Length - 1] == 1)
                return true;
            return false;
        }
        public static bool IsCTCP(IRCMessage Message)
        {
            if ((Message.Command.ToUpper().Trim() == "PRIVMSG" || Message.Command.ToUpper().Trim() == "NOTICE") && Message.Params.Count >= 2)
                return IsCTCP(Message.Params[1]);
            else
                return false;
        }

        public CTCPType Type;
        public string Command;
        //public List<string> Params;
        public string Params;

        public CTCPMessage()
        {
            Command = "";
            //Params = new List<string>();
            Params = "";
        }

        public CTCPMessage(IRCMessage Message)
        {
            Params = "WTF";
            Parse(Message);
        }


        public void Parse(IRCMessage Message)
        {
            if (!((Message.Command.ToUpper().Trim() == "PRIVMSG" || Message.Command.ToUpper().Trim() == "NOTICE") && Message.Params.Count >= 2))
                return;

            if (!IsCTCP(Message))
                return;

            switch (Message.Command.ToUpper().Trim())
            {
                case "PRIVMSG":
                    Type = CTCPType.CTCPQuery;
                    break;
                case "NOTICE":
                    Type = CTCPType.CTCPResponse;
                    break;
            }

            string Msg = Message.Params[1];

            if (Msg.Contains(" "))
            {
                Command = Msg.Substring(1, Msg.IndexOf(' ')).ToUpper().Trim();
                /*string Rest*/ this.Params = Msg.Substring(Command.Length + 2, (Msg.Length - Msg.IndexOf(' ')) - 2);
                Console.WriteLine("Params = {0}", this.Params);

                /*string LastParam = "";
                if (Rest.IndexOf(" :") != -1)
                    LastParam = Rest.Substring(Rest.IndexOf(" :") + 2).Trim();
                char[] ParamSplit = { ' ' };
                int ParmsTill = (Rest.IndexOf(" :") + 1) - (Rest.IndexOf(' ') + 1);
                if (ParmsTill < 0)
                    ParmsTill = Rest.Length - (Rest.IndexOf(' ') + 1);
                string[] Parms = Rest.Substring(Rest.IndexOf(' ') + 1, ParmsTill).Trim().Split(ParamSplit);
                foreach (string ThisParam in Parms)
                    if (ThisParam.Trim() != string.Empty)
                        Params.Add(ThisParam);
                if (LastParam != "")
                    Params.Add(LastParam);*/
            }
            else
            {
                Console.WriteLine("No params");
                Command = Msg.Substring(1, Msg.Length - 2);
            }
        }
    }
}
